package org.leon.controller;

import org.leon.common.Const;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.UUID;


@Controller
@RequestMapping("/user")
public class UserController {

    private final String TOKEN = "token";

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("name")String name, @RequestParam("password")String password, HttpSession session) {
        ModelAndView mv = new ModelAndView();
        if ("admin".equals(name) && "admin".equals(password)) {
            session.setAttribute(Const.SESSION_TOKEN, UUID.randomUUID());
            mv.setViewName("redirect:/resume/query");
        }else {
            mv.setViewName("login");
            mv.addObject("error", "Login Error: check user-name or password");
        }
        return mv;
    }

    @RequestMapping("/logout")
    public ModelAndView logout(HttpSession session) {
        session.removeAttribute(Const.SESSION_TOKEN);
        return new ModelAndView("login");
    }

    @RequestMapping("/home")
    public ModelAndView homePage() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return mv;
    }
}
